package com.cssw.fyzb.busi.api.msg;

import com.ydn.dbframe.plugin.activerecord.Page;
import com.cssw.fyzb.core.model.vo.msg.MsgVo;
import com.cssw.fyzb.busi.service.msg.MsgService;
import com.cssw.fyzb.core.model.dodb.SysMessage;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author
 */
@Function(description = "系统消息", parameters = {
        @Parameter(name = "pageNumber", type = Type.Integer, description = "当前页，从1开始", required = true),
        @Parameter(name = "pageSize", type = Type.Integer, description = "页大小", required = true),
})
@Component
public class ListMsg implements Action {

    private final static Logger logger = LoggerFactory.getLogger(ListMsg.class);

    @Autowired
    private MsgService msgService;

    @Override
    public Response execute(Request request) throws Exception {
        int pageNumber = request.getInteger("pageNumber");
        int pageSize = request.getInteger("pageSize");
        try {
            Page<SysMessage> page = msgService.listMsg(pageNumber, pageSize, SysMessage.STATE_EFFECTIVE);
            List<MsgVo> vos = new ArrayList<>();
            MsgVo msgVo = null;
            for (SysMessage s : page.getList()) {
                msgVo = new MsgVo();
                msgVo.setTitle(s.getTitle());
                msgVo.setCreateTime(s.getCreateTime());
                msgVo.setMsgContent(s.getContent());
                msgVo.setMsgId(s.getMsgId());
                vos.add(msgVo);
            }
            return Response.success().put("list", vos).put("total", page.getTotalPage()).put("count", page.getTotalRow());
        } catch (Exception e) {
            logger.info("", e);
            return Response.fail("操作异常");
        }
    }
}
